﻿/// -----------------------------------------------------------------------
/// (c) 2009 Microsoft Corporation -- All rights reserved
/// This code is licensed under the MS-PL
/// http://www.opensource.org/licenses/ms-pl.html
/// Courtesy of the Open Source Techology Center: http://port25.technet.com
/// -----------------------------------------------------------------------


using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using Microsoft.Win32.SafeHandles;

namespace SynthFastCGI {

    [StructLayout(LayoutKind.Sequential)]
    internal  struct PROCESS_INFORMATION {
        internal  IntPtr  hProcess;
        internal  IntPtr hThread;
        internal  int dwProcessId;
        internal  uint dwThreadId;
    }

    [StructLayout(LayoutKind.Sequential)]
    internal struct STARTUPINFO {
        internal  uint cb;
        internal  string lpReserved;
        internal  string lpDesktop;
        internal  string lpTitle;
        internal  uint dwX;
        internal  uint dwY;
        internal  uint dwXSize;
        internal  uint dwYSize;
        internal  uint dwXCountChars;
        internal  uint dwYCountChars;
        internal  uint dwFillAttribute;
        internal  uint dwFlags;
        internal  short wShowWindow;
        internal  short cbReserved2;
        internal  IntPtr lpReserved2;
        internal SafeFileHandle hStdInput;
        internal int hStdOutput;
        internal  int hStdError;
    }

    [StructLayout(LayoutKind.Sequential)]
    internal struct SECURITY_ATTRIBUTES {
        internal int length;
        internal IntPtr lpSecurityDescriptor;
        internal bool bInheritHandle;
    }

    internal class Win32 {

        internal const uint GENERIC_READ = (0x80000000);
        internal const uint GENERIC_WRITE = (0x40000000);
        internal const uint OPEN_EXISTING = 3;
        internal const uint FILE_FLAG_OVERLAPPED = (0x40000000);

        internal const int PIPE_ACCESS_INBOUND = 0x00000001;
        internal const int PIPE_ACCESS_OUTBOUND = 0x00000002;
        internal const int PIPE_ACCESS_DUPLEX = 0x00000003;

        //
        // Define the Named Pipe End flags for GetNamedPipeInfo
        //

        internal const int PIPE_CLIENT_END = 0x00000000;
        internal const int PIPE_SERVER_END = 0x00000001;

        //
        // Define the dwPipeMode values for CreateNamedPipe
        //

        internal const int PIPE_WAIT = 0x00000000;
        internal const int PIPE_NOWAIT = 0x00000001;
        internal const int PIPE_READMODE_BYTE = 0x00000000;
        internal const int PIPE_READMODE_MESSAGE = 0x00000002;
        internal const int PIPE_TYPE_BYTE = 0x00000000;
        internal const int PIPE_TYPE_MESSAGE = 0x00000004;
        internal const int PIPE_ACCEPT_REMOTE_CLIENTS = 0x00000000;
        internal const int PIPE_REJECT_REMOTE_CLIENTS = 0x00000008;

        //
        // Define the well known values for CreateNamedPipe nMaxInstances
        //

        internal const int PIPE_UNLIMITED_INSTANCES = 255;


        [DllImport("kernel32.dll", SetLastError=true)]
        internal  static extern SafeFileHandle CreateNamedPipe(String pipeName,uint dwOpenMode,uint dwPipeMode,uint nMaxInstances,uint nOutBufferSize,uint nInBufferSize,uint nDefaultTimeOut,ref  SECURITY_ATTRIBUTES lpSecurityAttributes);

        [DllImport("kernel32.dll", SetLastError=true)]
        internal  static extern int ConnectNamedPipe(SafeFileHandle hNamedPipe,IntPtr lpOverlapped);

        [DllImport("kernel32.dll", SetLastError=true)]
        internal  static extern SafeFileHandle CreateFile(String pipeName,uint dwDesiredAccess,uint dwShareMode,IntPtr lpSecurityAttributes,uint dwCreationDisposition,uint dwFlagsAndAttributes,IntPtr hTemplate);

        [DllImport("kernel32.dll")]
        internal static extern bool CreateProcess(string lpApplicationName, string lpCommandLine, IntPtr lpProcessAttributes, IntPtr lpThreadAttributes,bool bInheritHandles, uint dwCreationFlags, IntPtr lpEnvironment,string lpCurrentDirectory, ref STARTUPINFO lpStartupInfo, out PROCESS_INFORMATION lpProcessInformation);

        [DllImport("Kernel32.dll", ExactSpelling=true, CharSet=CharSet.Auto, SetLastError=true)]
        public static extern bool CloseHandle(IntPtr hHandle); 

    }
}
